package com.seio.edc.controller.customerMap;

import com.alibaba.fastjson.JSON;
import com.nova.edc.model.system.CustomerInfo;
import com.nova.edc.model.system.Login;
import com.nova.edc.model.system.MapArea;
import com.nova.edc.model.system.ModelConstant;
import com.nova.edc.service.energy.GatherSummaryService;
import com.nova.edc.service.energy.OverallStatisticsService;
import com.nova.edc.service.system.CustomerInfoService;
import com.nova.edc.service.system.CustomerMapService;
import com.nova.edc.service.system.CustomerRegionService;
import com.nova.edc.service.system.MapAreaService;
import com.seio.edc.util.IntegerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 客户地图
 */
@Controller
@RequestMapping(value = "/customerMap")
public class CustomerMapController {

    @Autowired
    private CustomerMapService novaCustomerMapService;

    @Autowired
    private CustomerRegionService novaCustomerRegionService;

    @Autowired
    private CustomerInfoService novaCustomerInfoService;

    @Autowired
    private MapAreaService mapAreaService;

    @Autowired
    private GatherSummaryService gatherSummaryService;

    @Autowired
    private OverallStatisticsService novaOverallStatisticsService;

    /**
     * 客户地图初期页面
     */
    @RequestMapping(value = "customerMapInit")
    public ModelAndView customerMapInit(HttpServletRequest request,
                                        HttpSession session, ModelMap modelMap) {
        Login login = (Login) session.getAttribute("sessionLogin");
        // 地图构建工具----获取所有省份信息
        MapArea mapArea = new MapArea();
        mapArea.setParentid(0);
        List<MapArea> map_provinces = mapAreaService.findMapAreaByExample(mapArea);
        modelMap.put("map_provinces", JSON.toJSONString(map_provinces));
        List<Map<String, Object>> positionList = null;
        if (login.getRoletype() != null && login.getRoletype() == ModelConstant.ROLE_GROUP) {
            positionList = novaCustomerMapService.findFirstCustomerMapGroup(login.getGroupcode());
        } else {
            positionList = novaCustomerMapService.findFirstCustomerMap(login.getUsername());
        }
        modelMap.put("positionList", JSON.toJSONString(positionList));
        modelMap.put("areaList", positionList);

        //获取全国耗电总量
        List<Map<String, Object>> energy = gatherSummaryService.findAllEnergy("", ModelConstant.MAP_DEFAULT_TIME_TYPE);
        BigDecimal lightEnergy = BigDecimal.ZERO;
        BigDecimal airEnergy = BigDecimal.ZERO;
        for (int i = 0; i < energy.size(); i++) {
            if (energy.get(i).get("devie").equals(ModelConstant.DEVICE_LIGHTENERGY)) {
                lightEnergy = (BigDecimal) energy.get(i).get("energy");
            } else if (energy.get(i).get("devie").equals(ModelConstant.DEVICE_AIR)) {
                airEnergy = (BigDecimal) energy.get(i).get("energy");
            }
        }
        BigDecimal allEnergy = lightEnergy.add(airEnergy);
        modelMap.put("lightEnergy", lightEnergy);
        modelMap.put("airEnergy", airEnergy);
        modelMap.put("allEnergy", allEnergy);
        modelMap.put("searchTime", ModelConstant.MAP_SERACHTIME);
        modelMap.put("timeType", ModelConstant.MAP_DEFAULT_TIME_TYPE);
        return new ModelAndView("customerMap/customerMapAll", modelMap);
    }

    /**
     * 根据区域获得耗电量
     */
    @RequestMapping(value = "getEnergy.shtml")
    @ResponseBody
    public List<String> getEnergy(HttpServletRequest request, HttpSession session, String area, ModelMap modelMap,
                                  String searchDate) {
        List<Map<String, Object>> energy = gatherSummaryService.findAllEnergy(area, Integer.parseInt(searchDate));
        BigDecimal lightEnergy = BigDecimal.ZERO;
        BigDecimal airEnergy = BigDecimal.ZERO;
        for (int i = 0; i < energy.size(); i++) {
            if (energy.get(i).get("devie").equals(ModelConstant.DEVICE_LIGHTENERGY)) {
                lightEnergy = (BigDecimal) energy.get(i).get("energy");
            } else if (energy.get(i).get("devie").equals(ModelConstant.DEVICE_AIR)) {
                airEnergy = (BigDecimal) energy.get(i).get("energy");
            }
        }
        BigDecimal allEnergy = lightEnergy.add(airEnergy);
        ArrayList<String> list = new ArrayList<String>();
        DecimalFormat df = new DecimalFormat("0.00");
        list.add(df.format(allEnergy));
        list.add(df.format(lightEnergy));
        list.add(df.format(airEnergy));
        return list;
    }

    /**
     * 查看客户地理位置
     */
    @RequestMapping(value = "searchPostion")
    public ModelAndView searchPostion(HttpServletRequest request,
                                      Integer customerId, HttpSession session, String companyRegion,
                                      ModelMap modelMap) {
        CustomerInfo customerInfo = novaCustomerInfoService.getById(customerId);
        modelMap.put("customerInfo", customerInfo);
        return new ModelAndView("customerMap/baseMap", modelMap);
    }

    /**
     * 根据省份获取所有下级单位信息
     */
    @RequestMapping(value = "findMapCityByProvince")
    @ResponseBody
    public List<MapArea> findMapCityByProvince(
            String province) {
        return mapAreaService.findCityAreaByProvince(province);
    }

    /**
     * 根据省份或上级单位获取下级单位数据
     */
    @RequestMapping(value = "findCityByProvince")
    @ResponseBody
    public List<Map<String, Object>> findCityByProvince(HttpSession session, String province) {
        Login login = (Login) session.getAttribute("sessionLogin");
        List<Map<String, Object>> positionList = novaCustomerMapService
                .findSecondCustomerMap(province, login);
        return positionList;
    }

    /**
     * 根据上级单位获取该单位所有客户
     */
    @RequestMapping(value = "findCustomerByCity")
    @ResponseBody
    public List<CustomerInfo> findCustomerByCity(HttpSession session, String city) {
        Login login = (Login) session.getAttribute("sessionLogin");
        List<CustomerInfo> customerInfos = novaCustomerInfoService.findCustomerByCity(login, city);
        return customerInfos;
    }

    /*
    *根据地点查询坐标
    */
    @RequestMapping(value = "findPointByArea")
    @ResponseBody
    public MapArea findPointByArea(String area) {
        MapArea mapArea = mapAreaService.getAreaByName(area);
        return mapArea;
    }

    @RequestMapping(value = "getEnergyArea")
    @ResponseBody
    public List<Map<String, Object>> getEnergyArea(HttpSession session, String position, Integer searchDate) {
        if (searchDate == null) {
            searchDate = ModelConstant.MAP_DEFAULT_TIME_TYPE;
        }
        List<Map<String, Object>> map = gatherSummaryService.getByMapArea(position, searchDate);
        return map;
    }
}
